package com.xr.blackhoole.redis;

import com.xr.blackhoole.mvc.configuration.BlackholeIsolationConfiguration;
import com.xr.blackhoole.mvc.context.BlackholeContext;
import com.xr.blackhoole.mvc.context.BlackholeContextHolder;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.lang.Nullable;

import javax.annotation.Resource;

/**
 *重写StringRedisSerializer ，更改key值
 */
public class KeyStringRedisSerializer extends StringRedisSerializer {

    @Resource
    private BlackholeIsolationConfiguration isolationConfiguration;

    public byte[] serialize(@Nullable String redisKey) {
        BlackholeContext context = BlackholeContextHolder.getContext();
        if (null != context) {
            redisKey = isolationConfiguration.generateIsolationKey(redisKey);
        }
        return super.serialize(redisKey);
    }
}
